/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_timer.h
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the timer module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-23  |V2.0     |Wang Junxiong|New creat
 *  |2025-08-12  |V2.1.1   |Wang Junxiong|T2 Divider 1, only using a 1:1 division
 *****************************************************************************/

#ifndef KF8A100FXX_TIMER_H
#define KF8A100FXX_TIMER_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx.h"
/******************************************************************************
 *                      Macro or Inline
 *****************************************************************************/

/**
 * @brief timer module disable/enable
 * 0: disable, 1:enable
 *
 */
#define TIM1_ENABLE(x) (T1ON = (x))
#define TIM2_ENABLE(x) (T2ON = (x))
#define TIM3_ENABLE(x) (T3ON = (x))
#define TIM4_ENABLE(x) (T4ON = (x))
/*****************************************************************************
 *                      Typedef Definitions
 *****************************************************************************/

/**
 * @brief Timer mode
 */
typedef enum
{
    /** @brief Timing Mode */
    TIMER_TIMING_MODE = 0u,
    /** @brief Counting mode */
    TIMER_COUNTING_MODE = 32u
} Tim_ModeType;

/**
 * @brief Timer trigger mode, rising or falling edge
 */
typedef enum
{
    /** @brief Rising edge trigger */
    TIMER_RISING_EDGE = 0u,
    /** @brief Falling edge trigger */
    TIMER_FALLING_EDGE = 16u
} Tim_EdgeType;

/**
 * @brief Timer reload enabled
 */
typedef enum
{
    TIMER_RELOAD_DISABLE = 0u,
    TIMER_RELOAD_ENABLE  = 128u
} Tim_ReloadType;

/**
 * @brief Timer0 prescale
 */
typedef enum
{
    TIMER0_PRESCALER_1   = 8u,
    TIMER0_PRESCALER_2   = 0u,
    TIMER0_PRESCALER_4   = 1u,
    TIMER0_PRESCALER_8   = 2u,
    TIMER0_PRESCALER_16  = 3u,
    TIMER0_PRESCALER_32  = 4u,
    TIMER0_PRESCALER_64  = 5u,
    TIMER0_PRESCALER_128 = 6u,
    TIMER0_PRESCALER_256 = 7u
} Tim0_PrescalerType;

/**
 * @brief Timer1 prescale
 */
typedef enum
{
    TIMER1_PRESCALER_1 = 0u,
    TIMER1_PRESCALER_2 = 16u,
    TIMER1_PRESCALER_4 = 32u,
    TIMER1_PRESCALER_8 = 48u
} Tim1_PrescalerType;

/**
 * @brief Timer 1 clock source
 */
typedef enum
{
    /** @brief High frequency internal crystal: 16MHz */
    TIMER1_SOURCE_INTHF = 1u,
    /** @brief System clock/4 */
    TIMER1_SOURCE_SCLKDIV4 = 0u
} Tim1_SourceType;

/**
 * @brief Timer 2 prescale 1
 */
typedef enum
{
    TIMER2_PRESCALER1_1  = 0u,
} Tim2_Prescaler1Type;

/**
 * @brief Timer 2 prescale 2
 */
typedef enum
{
    TIMER2_PRESCALER2_1  = 0u,
    TIMER2_PRESCALER2_2  = 8u,
    TIMER2_PRESCALER2_3  = 16u,
    TIMER2_PRESCALER2_4  = 24u,
    TIMER2_PRESCALER2_5  = 32u,
    TIMER2_PRESCALER2_6  = 40u,
    TIMER2_PRESCALER2_7  = 48u,
    TIMER2_PRESCALER2_8  = 56u,
    TIMER2_PRESCALER2_9  = 64u,
    TIMER2_PRESCALER2_10 = 72u,
    TIMER2_PRESCALER2_11 = 80u,
    TIMER2_PRESCALER2_12 = 88u,
    TIMER2_PRESCALER2_13 = 96u,
    TIMER2_PRESCALER2_14 = 104u,
    TIMER2_PRESCALER2_15 = 112u,
    TIMER2_PRESCALER2_16 = 120u
} Tim2_Prescaler2Type;

/**
 * @brief Timer 2 counting method and interrupt generation method
 */
typedef enum
{
    /** @brief Count up, interrupt flag on overflow */
    TIMER2_COUNT_UP_U = 0u,
    /** @brief Up-down counting, interrupt flag on overflow */
    TIMER2_COUNT_UPDOWN_U = 64u,
    /** @brief Up-down counting, interrupt flag on underflow */
    TIMER2_COUNT_UPDOWN_D = 128u,
    /** @brief Up-down counting, interrupt flag on overflow and underflow */
    TIMER2_COUNT_UPDOWN_B = 192u
} Tim2_CountDirectionType;

/**
 * @brief Timer 3 4 prescale
 */
typedef enum
{
    TIMER3_4_PRESCALER_1 = 0u,
    TIMER3_4_PRESCALER_2 = 16u,
    TIMER3_4_PRESCALER_4 = 32u,
    TIMER3_4_PRESCALER_8 = 48u
} Tim3_4_PrescalerType;

/**
 * @brief Timer 3 4 clock source
 */
typedef enum
{
    /** @brief Internal High Frequency Crystal */
    TIMER3_4_SOURCE_INTHF = 0u,
    /** @brief Internal low frequency crystal */
    TIMER3_4_SOURCE_INTLF = 2u,
    /** @brief External High Frequency Crystal */
    TIMER3_4_SOURCE_EXTHF = 4u,
} Tim3_4_SourceType;

/**
 * @brief Timer on/off state type
 */
typedef enum
{
    TIMER_OFF = 0u,
    TIMER_ON  = 1u,
} Tim_OnOffType;

/******************************************************************************
 *                      Export Variables
 *****************************************************************************/

/******************************************************************************
 *                      Export Functions
 *****************************************************************************/

/**
 * @brief T0 initialization
 *
 * @param Mode Timer mode
 * @param Edge Trigger Edge
 * @param Div prescale
 * @param T0InitialValue T0 initial value
 */
void Tim0_Init(Tim_ModeType Mode, Tim_EdgeType Edge, Tim0_PrescalerType Div, uint8_t T0InitialValue);

/**
 * @brief T1 initialization
 *
 * @param Mode Timer mode
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period PP2, PP1
 */
void Tim1_Init(Tim_ModeType Mode, Tim_ReloadType Reload, Tim1_PrescalerType Div, uint16_t Period);

/**
 * @brief T2 initialization
 *
 * @param Direction Counting direction
 * @param Div1 prescale1
 * @param Div2 prescale2
 * @param Period PP5H, PP5L
 */
void Tim2_Init(Tim2_CountDirectionType Direction, Tim2_Prescaler1Type Div1, Tim2_Prescaler2Type Div2, uint16_t Period);

/**
 * @brief T3 initialization
 *
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T3REH, T3REL
 */
void Tim3_Init(Tim_ReloadType Reload, Tim3_4_PrescalerType Div, uint16_t Period);

/**
 * @brief T4 initialization
 *
 * @param Reload Reload Enable
 * @param Div prescale
 * @param Period T4REH, T4REL
 */
void Tim4_Init(Tim_ReloadType Reload, Tim3_4_PrescalerType Div, uint16_t Period);

/**
 * @brief Setting the timer on/off state
 *
 * @param State 0:off，1:on
 */
void Tim1_SetState(Tim_OnOffType State);

/**
 * @brief Setting the timer on/off state
 *
 * @param State 0:off，1:on
 */
void Tim2_SetState(Tim_OnOffType State);

/**
 * @brief Setting the timer on/off state
 *
 * @param State 0:off，1:on
 */
void Tim3_SetState(Tim_OnOffType State);

/**
 * @brief Setting the timer on/off state
 *
 * @param State 0:off，1:on
 */
void Tim4_SetState(Tim_OnOffType State);

/**
 * @brief Setting the current value of T0
 *
 * @param T0InitialValue T0 initial value
 */
void Tim0_SetInitialValue(uint8_t T0InitialValue);

/**
 * @brief Setting the T1 cycle value
 *
 * @param Period PP2, PP1
 */
void Tim1_SetPeriodValue(uint16_t Period);

/**
 * @brief Setting the T2 cycle value
 *
 * @param Period PP5H, PP5L
 */
void Tim2_SetPeriodValue(uint16_t Period);

/**
 * @brief Setting the T3 cycle value
 *
 * @param Period T3REH, T3REL
 */
void Tim3_SetPeriodValue(uint16_t Period);

/**
 * @brief Setting the T4 cycle value
 *
 * @param Period T4REH, T4REL
 */
void Tim4_SetPeriodValue(uint16_t Period);

/**
 * @brief Timer 0 deinitialization
 *
 */
void Tim0_DeInit(void);

/**
 * @brief Timer 1 deinitialization
 *
 */
void Tim1_DeInit(void);

/**
 * @brief Timer 2 deinitialization
 *
 */
void Tim2_DeInit(void);

/**
 * @brief Timer 3 deinitialization
 *
 */
void Tim3_DeInit(void);

/**
 * @brief Timer 4 deinitialization
 *
 */
void Tim4_DeInit(void);
#ifdef __cplusplus
}
#endif

#endif
/* EOF */
